const {app, BrowserWindow, BrowserView, ipcMain, session, net, webContents, remote, Tray, Menu, dialog, shell} = require('electron')
const path = require('path')
// const { dialog } = require('electron-dialog');
const xlsx = require('xlsx');
const fs = require('fs');
const querystring = require('querystring');
// const {spawn, execFile} = require('child_process');
// const robot = require("robotjs");

const schedule = require("node-schedule");
const Base64 = require('js-base64').Base64;
const urlparse = require('url');

// const http = require('http');
// const https = require('https');
const logger = require('./logger')
const system = require('./system')
const httpServer = require('./httpServer')
const webSocketServer = require('./webSocketServer')
const netRequest = require('./netRequest')
const dao = require('./dao')
const service = require('./service/service')
const userService = require('./service/userService')
const liveRoomService = require('./service/liveRoomService')
const jsExpress = require('./jsExpress')
// const exportUtils = require('./exportUtils')
const robot = require('./robot')
const config = require('./../config')
const utils = require('./utils')

const api_base_path = 'http://localhost:28081/'
const yzm_base_path = 'http://192.168.2.158:8000/'


//全局变量区域,后面通过界面操作
let rootPath = path.resolve(__dirname);
var proxy = false;    //是否代理
var r_cookie = false;    //是否需要远程cookie
var check_task_timer = '*/10 * * * * *';
var monitor_win_timer = '*/2 * * * * *';
let monitorWinPoolRun = false;
let devModel = false;
let winTimeOut = 20 * 60 * 1000;  //采集窗口默认超时时间 30分钟
let token = '';  //登录token
let mainWindow = null;
// let mainWindow = null;
const WEBVIEW_TOP = 200;
const WEBVIEW_HEIGHT = 580;
const WEBVIEW_WIDTH = 1024;

let winPoolArray = [];
let checkTaskJob = null;
let monitorWinJob = null;
let userData = {};
let IS_LOGIN = false;

function start(options) {
    logger.info(`core start params=${options}`);

    rootPath = options['rootPath'] ? options['rootPath'] : rootPath;
    check_task_timer = options['check_task_timer'] ? options['check_task_timer'] : check_task_timer;
    // monitor_win_timer = options['monitor_win_timer'] ? options['monitor_win_timer'] : monitor_win_timer;
    //devModel = options['devModel'] ? options['devModel'] : devModel;
    r_cookie = options['r_cookie'] ? options['r_cookie'] : r_cookie;
    proxy = options['proxy'] ? options['proxy'] : proxy;
    mainWindow = options['mainWindow'] ? options['mainWindow'] : mainWindow;

    createTable();  //sqlite 初始化业务表

    // startWebSocketServer(8765);

    startHttpServer(28081);

    let checkTaskRun = false;
    checkTaskJob = schedule.scheduleJob(check_task_timer, function () {

        if (!IS_LOGIN) {  //登录成功后才检测
            return;
        }
        console.debug("checkTaskJob start ...");
        if (checkTaskRun) {
            console.warn("checkTaskJob still start ...");
            return;
        } else {
            checkTaskRun = true;
        }
        try {
            userService.checkUserStatusTask(userData.token, function (r) {
                if (!r.success) {
                    logger.info(`user status check is expired，user=${userData.user}`)
                    mainWindow.loadFile('web/index.html').then(function () {
                        //core.indexPageLoaded();
                    });
                }

            });
        } catch (e) {
            console.error(e)
        } finally {
            checkTaskRun = false;
        }
    });


    // //监控窗口
    // monitorWinJob = schedule.scheduleJob(monitor_win_timer, function () {
    //     console.debug("monitor monitorWinPool start ...winSize=" + winPoolArray.length);
    //     if(monitorWinPoolRun){
    //         console.warn("monitor monitorWinPool still start ...winSize=" + winPoolArray.length);
    //         return;
    //     }else{
    //         monitorWinPoolRun = true;
    //     }
    //     try{
    //         monitorWinPool();
    //     }catch (e) {
    //         console.log(e)
    //     }finally {
    //         monitorWinPoolRun = false;
    //     }
    // });

}

function createTable() {
    // user,pwd,token,status(1 正常；0 失效),enable(1 启用；0 未启用)
    let sql = 'CREATE TABLE IF NOT EXISTS user (' +
        'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
        'create_time INT,' +
        'update_time INT,' +
        'user TEXT,' +
        'pwd TEXT,' +
        'status INT,' +
        'active INT,' +
        'role TEXT' +
        ')';

    let result = dao.execute(sql);
    console.log('result:', result);
    //
    // // #评论表
    // // batch_no,csrfs,article_urls,content,success_num,failed_num,total_num,status(0 草稿；1 已提交)
    // sql = 'CREATE TABLE IF NOT EXISTS comment (' +
    //     'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
    //     'create_time INT,' +
    //     'update_time INT,' +
    //     'batch_no TEXT,' +
    //     'token_users TEXT,' +
    //     'article_urls TEXT,' +
    //     'content TEXT,' +
    //     'status INT,' +
    //     'total_num INT,' +
    //     'success_num INT,' +
    //     'failed_num INT' +
    //     ')';
    //
    // result = dao.execute(sql);
    // console.log('result:', result);
    //
    // // #评论历史
    // // batch_no,aid,csrf,article_url,content,next_exec_time(下次执行时间),status(0 待评论； 1 评论成功；2 评论失败),error_message
    // sql = 'CREATE TABLE IF NOT EXISTS comment_detail (' +
    //     'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
    //     'create_time INT,' +
    //     'update_time INT,' +
    //     'batch_no TEXT,' +
    //     'aid TEXT,' +
    //     'token_user TEXT,' +
    //     'article_url TEXT,' +
    //     'content TEXT,' +
    //     'next_exec_time INT,' +
    //     'status INT,' +
    //     'error_message TEXT' +
    //     ')';
    //
    // result = dao.execute(sql);
    // console.log('result:', result);
}

function startWebSocketServer(port) {
    webSocketServer.startWebSocket(port, function (message, sid) {
        //判断message 指令 做出相关动作 todo
        // debugger
        let res = JSON.parse(message);
        if (res.type == 'ping') {
            webSocketServer.sendMessage('pong', '', sid);
        } else if (res.type == 'showOrHideWebview') {
            //获取webview
            var winObject = getWinObjectByWebContentsId(sid);
            if (!winObject) {
                return;
            }
            let webview = winObject.webview;
            if (res.message.value) {
                webview.setBounds({x: 0, y: WEBVIEW_TOP, width: WEBVIEW_WIDTH, height: WEBVIEW_HEIGHT})

            } else {
                webview.setBounds({x: 0, y: 0, width: WEBVIEW_WIDTH, height: 50})
            }
        } else if (res.type == 'collectStatus') {
            let collectStart = res.message.collectStart;  //todo 还需要刷新页面，看还要不要处理
            let processTask = getProcessTaskByWebContentsId(sid);
            if (processTask != null) {
                processTask.status = collectStart ? 1 : -1;
            }
        }
        // console.log(message);

    })

}

function startHttpServer(port) {
    //http server
    httpServer.startHttpServer(port, async function (options) {
        console.log('http options path:', options.path);
        let req = options.req;
        let res = options.res;
        let path = options.path;
        let jsonData = options.jsonData;
        // const parsedUrl = url.parse(req.url);
        // const path = parsedUrl.pathname;
        const query = options.query;
        let result = {success: true, message: '操作成功'}
        // const jsonData = JSON.parse(requestData);
        if (path.includes('/system/liveStatus')) {  //健康状态
            result.data = 'OK'
            //console.log('createWinWithProcessTask', activeProcessId)

        } else if (path.includes('/system/configList')) {
            result.data = service.dataConfigList();

        } else if (path.includes('/system/dataOptionList')) {
            result.data = service.dataOptionList();

        } else if (path.includes('/system/getApplicationData')) {  //
            result.data = userService.getApplicationData();

        } else if (path.includes('/system/setApplicationData')) {  //
            result.data = userService.setApplicationData(jsonData);
        } else if (path.includes('/system/openWin')) { //从源站获取token,模拟进行
            result.data = 'OK'
            createWin(jsonData);
        }else if (path.includes('/system/openWx')) { //从源站获取token,模拟进行
            result.data = 'OK'
            //createWin(jsonData);
            // exportToExcel();


            function parseCurlCommand(curlCommand) {
                const parsed = {
                    url: '',
                    headers: {},
                    data: ''
                };

                // 提取 URL
                const urlRegex = /curl.*?'(http[^']+)'/
                const urlMatch = curlCommand.match(urlRegex);
                if (urlMatch) {
                    parsed.url = urlMatch[1];
                }

                // 提取请求头
                const headerRegex = /-H '([^']+)'/g;
                let headerMatch;
                while ((headerMatch = headerRegex.exec(curlCommand)) !== null) {
                    const headerParts = headerMatch[1].split(':');
                    const key = headerParts[0].trim();
                    const value = headerParts[1].trim();
                    parsed.headers[key] = value;
                }

                // 提取请求体
                const dataRegex = /-d '([^']+)'/;
                const dataMatch = curlCommand.match(dataRegex);
                if (dataMatch) {
                    parsed.data = dataMatch[1];
                }

                return parsed;
            }

            const curlCommand = jsonData.url;
            const parsedData = parseCurlCommand(curlCommand);
            let rank_url = parsedData.url;
            let headers = parsedData.headers;
            let json_data = JSON.parse(parsedData.data);
            if(rank_url.endsWith('/_alive/bff_h5/msg/list')){
                var commentType = 0;
                if(jsonData.commentStatus){
                    commentType = 2;
                }
                console.log('commentType='+commentType)
                liveRoomService.request(commentType,rank_url,json_data,headers,[],function(requestResult){
                    // console.log(requestResult)
                    // uid:msg_data.uid,nickname:msg_data.wx_nickname,content:msg_data.org_msg_content,create_time:msg_data.created_at

                    let dataToExport = [['昵称','内容','时间']];
                    for (let i = 0; i < requestResult.length; i++) {
                        let rrData = requestResult[i];
                        dataToExport.push([rrData.nickname,rrData.content,utils.timestampFormat(rrData.create_time,'YYYY-MM-DD HH:mm:ss')])
                    }
                    exportToExcel(mainWindow,dataToExport,'comments_wx.xlsx',function(){
                        // crawlerWin.close();
                    })
                    // let dataToExport = [['昵称','排名','时间']];
                    // for (let i = 0; i < requestResult.length; i++) {
                    //     let rrData = requestResult[i];
                    //     dataToExport.push([rrData.nickname,rrData.rank,rrData.time])
                    // }
                    // exportToExcel(mainWindow,dataToExport,'comments_wx.xlsx',function(){
                    //     //crawlerWin.close();
                    // })
                });

            }else{
                liveRoomService.request(1,rank_url,json_data,headers,[],function(requestResult){
                    // console.log(requestResult)
                    // uid:msg_data.uid,nickname:msg_data.wx_nickname,content:msg_data.org_msg_content,create_time:msg_data.created_at

                    let dataToExport = [['昵称','排名','时间']];
                    for (let i = 0; i < requestResult.length; i++) {
                        let rrData = requestResult[i];
                        dataToExport.push([rrData.nickname,rrData.rank,rrData.time])
                    }
                    exportToExcel(mainWindow,dataToExport,'ranks_wx.xlsx',function(){
                        //crawlerWin.close();
                    })
                });

            }
            // let rank_url = 'https://appcwg9qu6b2301.h5.xiaoeknow.com/xe.data.learn_center.get_user_rank_list/1.0.0'
            // let json_data = {
            //     'bizData': {
            //         'page_size': 10,
            //         'page': 1,
            //         'rank_type': 1,
            //     },
            // }
            // let cookies = jsonData.ck.replaceAll('\n',';');
            // var headers = {
            //     'Reqable-Id': 'reqable-id-ce7d0481-b4c9-447e-9d78-cf72a8cc57f4',
            //     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/6.8.0(0x16080000) MacWechat/3.8.7(0x13080711) XWEB/1191 Flue1111',
            //     'Accept': 'application/json, text/plain, */*',
            //     'Content-Type': 'application/json',
            //     'origin': 'https://appcwg9qu6b2301.h5.xiaoeknow.com',
            //     'sec-fetch-site': 'same-origin',
            //     'sec-fetch-mode': 'cors',
            //     'sec-fetch-dest': 'empty',
            //     'accept-language': 'en-US,en;q=0.9',
            //     'Cookie': 'xenbyfpfUnhLsdkZbX=0; sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%2218ddb273083a05-02a4028631c275-506e3c26-1024000-18ddb27308415%22%7D; sa_jssdk_2015_appcwg9qu6b2301_h5_xiaoeknow_com=%7B%22distinct_id%22%3A%22u_65d0801abdc60_aiLH15bzwo%22%2C%22first_id%22%3A%2218ddb273083a05-02a4028631c275-506e3c26-1024000-18ddb27308415%22%2C%22props%22%3A%7B%7D%7D; shop_version_type=4; ko_token=6be038b208faa07d8ccc7913e44323b6; logintime=1709899175'
            // };


        } else if (path.includes('/user/loginSuccess')) {  //客户端服务接口
            result.data = jsonData;
            userData = jsonData;
            IS_LOGIN = true
        } else if (path.includes('/user/checkStatus')) {
            result.data = userService.checkStatus(jsonData);

        } else if (path.includes('/user/login')) {
            result.data = userService.login(jsonData);

        } else if (path.includes('/user/list')) {  //token 列表
            result.data = userService.listByPage(query);
        } else if (path.includes('/user/save')) {  //token save
            result.data = userService.save(jsonData);
        } else if (path.includes('/user/update')) {  //token save
            result.data = userService.update(jsonData);
        } else if (path.includes('/user/del')) {  //token save
            result.data = userService.deleteById(jsonData);
        } else if (path.includes('/system/openExtUrl')) {
            let extUrl = jsonData.extUrl;
            shell.openExternal(extUrl);
        } else {
            throw new Error("未找到该请求," + path);
        }
        return result;

    })
}

// function getTaskByWebContentsId(webContentsId) {
//     let winObj = winPoolArray.find((item) => item['webContentsId'] == webContentsId);
//     if (winObj == null) {
//         return null;
//     }
//     return winObj.processTask.currentTask;
// }
//
// function getProcessTaskByWebContentsId(webContentsId) {
//     let winObj = winPoolArray.find((item) => item['webContentsId'] == webContentsId);
//     if (winObj == null) {
//         return null;
//     }
//     return winObj.processTask;
// }
//
function getWinObjectByWebContentsId(webContentsId) {
    let winObject = winPoolArray.find((item) => item['webContentsId'] == webContentsId);
    return winObject;
}

//
// function getProcessTaskByActiveProcessId(activeProcessId) {
//     let winObj = winPoolArray.find((item) => item['activeProcessId'] == activeProcessId);
//     if (winObj == null) {
//         return null;
//     }
//     return winObj.processTask;
// }
//
//
// function removeCurrentTaskByWebContentsId(webContentsId) {
//     let winObj = winPoolArray.find((item) => item['webContentsId'] == webContentsId);
//     if (winObj == null) {
//         return;
//     }
//     let task = winObj.processTask.currentTask;
//     winObj.processTask.taskDataArray.remove('id', task.id);
// }

Array.prototype.remove = function (prop, value) {
    for (let i = 0; i < this.length; i++) {
        if (this[i][prop] === value) {
            this.splice(i, 1);
            i--; // 更新索引，以便继续遍历
        }
    }
};


Array.prototype.removeFirst = function (prop, value) {
    for (let i = 0; i < this.length; i++) {
        if (this[i][prop] === value) {
            return this.splice(i, 1)[0];
        }
    }
    return null;
};

// function genNonDuplicateID() {
//     // let idStr = Date.now().toString(36)
//     // idStr += Math.random().toString(36).substr(3)
//     // return idStr;
//
//     const timestamp = new Date().getTime(); // 获取当前时间戳
//     const random = Math.floor(Math.random() * 10000); // 生成随机数
//     const id = timestamp * 10000 + random; // 组合时间戳和随机数
//     return id;
// }
//
// async function getProxies(activeProcessId) {
//     var url = api_base_path + 'crawler/crawlerTask/getProxies?activeProcessId=' + activeProcessId;
//     var promiseRequest = netRequest.request(url);
//     var result = await promiseRequest;
//     // console.log(result)
//     var res = JSON.parse(result);
//     const proxyConfig = {
//         proxyRules: 'http://' + res.data
//         // proxyBypass: 'localhost', // Add more bypass addresses as needed
//     };
//     console.log(proxyConfig)
//     return proxyConfig;
//
// }

// async function getCookies(activeProcessId) {
//     var url = api_base_path + 'crawler/crawlerTask/getCookies?activeProcessId=' + activeProcessId;
//     var promiseRequest = netRequest.request(url);
//     var result = await promiseRequest;
//     return JSON.parse(result).data;
// }
//
//
// function clearCookie(url) {
//     console.log('clear cookies url=' + url)
//     // 删除cookie
//     session.defaultSession.cookies.get(
//         {url: url},
//         (err, cookies) => {
//             console.log(cookies);
//             //删除cookie需要循环remove
//             for (var i = 0; i < cookies.length; i++) {
//                 //删除cookie
//                 session.defaultSession.cookies.remove(
//                     url,
//                     cookies[i].name,
//                     (error) => {
//                         //判断是否error
//                     }
//                 );
//             }
//         }
//     );
// }
//
// // 设置请求的 Cookie
// function setRequestCookie(webContents, requestUrl, cookieString) {
//     const parsedUrl = urlparse.parse(requestUrl);
//     var url = parsedUrl.protocol + '//' + parsedUrl.hostname;
//     // 获取当前页面的 session
//     const ses = webContents.session;
//
//     // 解析 Cookie 字符串并设置到 session
//     console.log('cookiestr before=' + cookieString)
//     cookieString.split(';').map(cookie => {
//         const [name, value] = cookie.trim().split('=');
//         var ck = {url: url, name, value};
//         ses.cookies.set(ck, (error) => {
//             if (error) {
//                 console.error('Failed to set cookies:', error);
//             } else {
//                 console.log('Cookies set successfully!');
//             }
//         });
//     });
//     // 获取当前窗口的 cookies
//     ses.cookies.get({url: url}).then((cookies) => {
//         var ckarray = cookies.map(ck => {
//             return ck.name + "=" + ck.value
//         })
//         console.log('Cookies after:', ckarray.join(";"));
//     }).catch((error) => {
//         console.error('Error getting cookies:', error);
//     });
// }


function createWin(bizParams) {
    const crawlerWin = new BrowserWindow({
        width: 1024, height: 800,
        webPreferences: {
            webviewTag: true,
            preload: path.join(rootPath, 'preload-new.js')
        }
    })


    // 获取默认会话
    // const ses = session.defaultSession;

    // // 清空所有Cookie
    // ses.clearStorageData({
    //     storages: ['cookies'],
    //     quotas: []
    // });
    // // 清空本地存储
    // ses.clearStorageData({
    //     storages: ['appcache', 'filesystem', 'indexdb', 'localstorage', 'shadercache', 'websql', 'serviceworkers'],
    //     quotas: []
    // });


    const webview = crawlerWin;

    // crawlerWin.setBrowserView(webview)
    // // webview.setBounds({x: 0, y: WEBVIEW_TOP, width: WEBVIEW_WIDTH, height: WEBVIEW_HEIGHT})
    // webview.setBounds({x: 0, y: 0, width: 1024, height: 800})
    // webview.setAutoResize({width: true,height:true});
    webview.webContents.loadURL(bizParams.url).then(async () => {
        crawlerWin.webContents.executeJavaScript(`
          const loadingDiv = document.createElement('div');
          loadingDiv.innerHTML = '数据处理中，请稍后..';
          loadingDiv.style.position = 'fixed';
          loadingDiv.style.top = '10%';
          loadingDiv.style.left = '50%';
          loadingDiv.style.transform = 'translate(-50%, -50%)';
          loadingDiv.style.background = 'rgba(255, 255, 255, 0.8)';
          loadingDiv.style.padding = '10px';
          loadingDiv.style.borderRadius = '10px';
          loadingDiv.style.zIndex = '9999';
          document.body.appendChild(loadingDiv);
        `);
    });

    try {
        webview.webContents.debugger.attach('1.3');
    } catch (err) {
        console.log('Debugger attach failed: ', err);
    }

    webview.webContents.debugger.on('detach', (event, reason) => {
        console.log('Debugger detached due to: ', reason);
    });


    let requestMap = {}
    let webContentsId = webview.webContents.id;

    // crawlerWin.loadFile("collectWin.html",{ search:"webContentsId="+webContentsId });
    //需要处理的请求有：
    // let needProcessUrls = [
    //     'https://appgpgezlza6405.h5.xiaoeknow.com/_alive/bff_h5/msg/list',
    //     'https://apphyamvcvu5184.h5.xiaoeknow.com/xe.data.learn_center.get_user_rank_list/1.0.0',
    // ]
    let needProcessUrls = [
        '/_alive/bff_h5/msg/list',
        '/xe.data.learn_center.get_user_rank_list/1.0.0',
    ]


    let requestInfoMap = {};


    session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
        let needProcess = false;
        let interceptorUrl = details.url;
        let needProcessUrl = null;
        for (let i = 0; i < needProcessUrls.length; i++) {
            needProcessUrl = needProcessUrls[i];
            if (interceptorUrl.endsWith(needProcessUrl)) {
                // callback({});
                needProcess = true;
                break;
            }

        }
        if (!needProcess) {
            callback({});
            return;
        }
        // var task = getTaskDataByWebContentsId(details.webContentsId);
        // if(!task || task.processUrls.includes(details.url)){
        //     callback({});
        //     return;
        // }
        // if(!task.extractDataUrl){
        //     callback({});
        //     return;
        // }
        // if(task.extractDataUrl && !details.url.startsWith(task.extractDataUrl)){
        //     callback({});
        //     return;
        // }
        // var method = details.method;
        // if(!(method.toLowerCase() == 'get' || method.toLowerCase() == 'post' || method.toLowerCase() == 'put')){
        //     callback({});
        //     return;
        // }

        var params = '';
        let uploadData = details.uploadData;
        for (var i = 0; i < uploadData.length; i++) {
            var uploadDataItem = uploadData[i];
            params += Buffer.from(uploadDataItem.bytes).toString();
        }
        // if(details.method == 'get' || details.method == 'GET'){
        console.log('sendNetRequest:', details.url);
        var fullUrl = details.url;
        const currentTimeStamp = Date.now();
        if (fullUrl.indexOf("?") > 0) {
            fullUrl = fullUrl + "&usefullc_zt=" + currentTimeStamp
        } else {
            fullUrl = fullUrl + "?usefullc_zt=" + currentTimeStamp
        }

        var commentType = 0;
        if(bizParams.commentStatus){
            commentType = 2;
        }
        console.log('commentType='+commentType)
        requestInfoMap[needProcessUrl] = {postData:params,headers:details.requestHeaders,fullUrl:fullUrl,method:details.method,commentType:commentType};

        // task.processUrls.push(fullUrl);
        // var webContentsId = details.webContentsId;
        // // 解析 URL
        // // const parsedUrl = urlparse.parse(fullUrl);
        // // sendHttpRequest(fullUrl,details.requestHeaders,function(result){
        //
        // sendNetRequest(fullUrl,method,details.requestHeaders,params,function(result){
        //     var data = result.data;
        //     if(result.success){
        //         // const responseBody = data.toString('utf-8');
        //         var task = getTaskDataByWebContentsId(webContentsId);
        //         var taskData = task.taskData;
        //         var webContents = task.winObject.instance.webContents;
        //         executeExtractRequestJs(taskData,data,webContents,function(resultData){
        //             resultData.url = fullUrl;
        //             resultData.clientTaskId = task.id,
        //                 submitTaskResult(resultData,webContentsId,function(){});  //暂时每次一次
        //         });
        //     }else{
        //         console.log(data);
        //     }
        // })
        // // }
        callback({});
    });

    //needProcessUrls = []
    webview.webContents.debugger.on('message', (event, method, params) => {
        if (method === 'Network.responseReceived') {

            let interceptorUrl = params.response.url;


            // console.log(params.type,interceptorUrl);
            // var processTask = getProcessTaskByWebContentsId(webContentsId);
            // if (!processTask) {
            //     // callback({});
            //     return;
            // }
            // if (!processTask.extractDataUrl) {
            //     // callback({});
            //     return;
            // }
            let needProcess = false;
            for (let i = 0; i < needProcessUrls.length; i++) {
                let needProcessUrl = needProcessUrls[i];
                if (interceptorUrl.endsWith(needProcessUrl)) {
                    // callback({});
                    needProcess = true;
                    break;
                }

            }
            if (!needProcess) {
                return;
            }
            if (params.type === 'XHR' || params.type === 'Fetch' || params.type == 'Script') {
                requestMap[params.requestId] = params.response.url;

            }

        } else if (method === 'Network.loadingFinished') {
            if (requestMap[params.requestId]) {
                try {
                    webview.webContents.debugger.sendCommand('Network.getResponseBody', {requestId: params.requestId}).then(async function (response) {

                        let live_room_url = '/_alive/bff_h5/msg/list';
                        let rank_url = '/xe.data.learn_center.get_user_rank_list/1.0.0';
                        if (requestMap[params.requestId].endsWith(live_room_url)) {
                            let requestInfo = requestInfoMap[live_room_url];
                            let postData = requestInfo.postData;
                            let headers = requestInfo.headers;
                            let commentType = requestInfo.commentType;
                            let resBody = response.base64Encoded ? Buffer.from(response.body, 'base64').toString() : response.body;
                            let resBodyJson = JSON.parse(resBody);
                            if (resBodyJson.code == 0) {
                                liveRoomService.request(commentType,requestMap[params.requestId],JSON.parse(postData),headers,[],function(requestResult){
                                    // console.log(requestResult)
                                    // uid:msg_data.uid,nickname:msg_data.wx_nickname,content:msg_data.org_msg_content,create_time:msg_data.created_at

                                    let dataToExport = [['昵称','内容','时间']];
                                    for (let i = 0; i < requestResult.length; i++) {
                                        let rrData = requestResult[i];
                                        dataToExport.push([rrData.nickname,rrData.content,utils.timestampFormat(rrData.create_time,'YYYY-MM-DD HH:mm:ss')])
                                    }
                                    exportToExcel(crawlerWin,dataToExport,'comments.xlsx',function(){
                                        crawlerWin.close();
                                    })
                                });
                            }


                        } else if (requestMap[params.requestId].endsWith(rank_url)) {
                            let requestInfo = requestInfoMap[rank_url];
                            let postData = requestInfo.postData;
                            let headers = requestInfo.headers;
                            let resBody = response.base64Encoded ? Buffer.from(response.body, 'base64').toString() : response.body;
                            let resBodyJson = JSON.parse(resBody);
                            if (resBodyJson.code == 0) {
                                liveRoomService.request(1,requestMap[params.requestId],JSON.parse(postData),headers,[],function(requestResult){
                                    // console.log(requestResult)
                                    // uid:msg_data.uid,nickname:msg_data.wx_nickname,content:msg_data.org_msg_content,create_time:msg_data.created_at

                                    let dataToExport = [['昵称','排名','时间']];
                                    for (let i = 0; i < requestResult.length; i++) {
                                        let rrData = requestResult[i];
                                        dataToExport.push([rrData.nickname,rrData.rank,rrData.time])
                                    }
                                    exportToExcel(crawlerWin,dataToExport,'ranks.xlsx',function(){
                                        crawlerWin.close();
                                    })
                                });
                            }


                        }
                        // console.log(response);
                        // console.log(1111);
                        // console.log(requestMap[params.requestId])
                        // var data = JSON.parse(response.base64Encoded ? Buffer.from(response.body, 'base64').toString():response.body)

                        // const responseBody = data.toString('utf-8');

                        // var processTask = getProcessTaskByWebContentsId(webContentsId);
                        // var webContents = processTask.winObject.webview.webContents;
                        // jsExpress.executeExtractRequestJs(processTask, data, webContents, function (resultData) {
                        //     // resultData.url = task.extractDataUrl;
                        //     // resultData.id = task.id;,
                        //     resultData.id = processTask.currentTask.id;
                        //     submitTaskResult(resultData, webContentsId, 1);  //暂时每次一次
                        // });
                    });

                } catch (e) {
                    console.error(e);
                }
            }
        }
    });

    webview.webContents.debugger.sendCommand('Network.enable');

    if (devModel) {
        //webview.webContents.openDevTools({mode: 'bottom'});
        crawlerWin.webContents.openDevTools({mode: 'bottom'});
    }


    //crawlerWin.webContents.openDevTools({ mode:'bottom'});
    crawlerWin.maximize();
    let now = new Date().getTime();
    let winObject = {
        id: genNonDuplicateID(),
        data: bizParams,
        // activeProcessId: activeProcessId,
        active: false,
        createTime: now,
        useTime: null,
        instance: crawlerWin,
        webview: webview,
        webContentsId: webview.webContents.id
    }
    winPoolArray.push(winObject);

    // 监听窗口关闭事件
    crawlerWin.on('close', (event) => {
        //closeCollectWindow(winObject,event);
    });

    return winObject;

}

function genNonDuplicateID() {
    // let idStr = Date.now().toString(36)
    // idStr += Math.random().toString(36).substr(3)
    // return idStr;

    const timestamp = new Date().getTime(); // 获取当前时间戳
    const random = Math.floor(Math.random() * 10000); // 生成随机数
    const id = timestamp * 10000 + random; // 组合时间戳和随机数
    return id;
}

function initMainWindow() {
    logger.info(`initMainWindow start`);
    try {
        mainWindow.webContents.debugger.attach('1.3');
    } catch (err) {
        console.log('Debugger attach failed: ', err);
    }

    mainWindow.webContents.debugger.on('detach', (event, reason) => {
        console.log('Debugger detached due to: ', reason);
    });

    mainWindow.webContents.debugger.sendCommand('Network.enable');

    let requestMap = {}
    let webContentsId = mainWindow.webContents.id;

    // crawlerWin.loadFile("collectWin.html",{ search:"webContentsId="+webContentsId });
    //需要处理的请求有：
    let needProcessUrls = [
        'https://appgpgezlza6405.h5.xiaoeknow.com/_alive/bff_h5/msg/list',
        'https://apphyamvcvu5184.h5.xiaoeknow.com/xe.data.learn_center.get_user_rank_list/1.0.0',
    ]
    mainWindow.webContents.debugger.on('message', (event, method, params) => {
        if (method === 'Network.responseReceived') {

            let interceptorUrl = params.response.url;


            // console.log(params.type,interceptorUrl);
            // var processTask = getProcessTaskByWebContentsId(webContentsId);
            // if (!processTask) {
            //     // callback({});
            //     return;
            // }
            // if (!processTask.extractDataUrl) {
            //     // callback({});
            //     return;
            // }
            let needProcess = false;
            for (let i = 0; i < needProcessUrls.length; i++) {
                let needProcessUrl = needProcessUrls[i];
                if (interceptorUrl.startsWith(needProcessUrl)) {
                    // callback({});
                    needProcess = true;
                    break;
                }

            }
            if (!needProcess) {
                return;
            }
            if (params.type === 'XHR' || params.type === 'Fetch' || params.type == 'Image') {
                requestMap[params.requestId] = params.response.url;

            }

        } else if (method === 'Network.loadingFinished') {
            if (requestMap[params.requestId]) {
                try {
                    mainWindow.webContents.debugger.sendCommand('Network.getResponseBody', {requestId: params.requestId}).then(async function (response) {

                        //登录后写入文件
                        if (
                            requestMap[params.requestId].startsWith('https://account.mobvoi.com/v3/login') ||
                            requestMap[params.requestId].startsWith('https://account.mobvoi.com/token/update') ||
                            requestMap[params.requestId].startsWith('https://moyin-gateway.moyin.com/moyin-account/v1/user/loginWithPassword')) {
                            // if(requestMap[params.requestId].startsWith('https://moyin-gateway.moyin.com/status/maintain')){
                            var data = response.base64Encoded ? Buffer.from(response.body, 'base64').toString() : response.body;
                            console.log(data)
                            let dataJson = JSON.parse(data);
                            if (dataJson.err_code == 0 || dataJson.errCode == 0) {
                                await system.sleep(5000);  //暂停5秒钟再获取，防止系统cookie还没有来得及设置
                                let processTask = {status: 1, analyseExpressType: 0,};
                                let uiEventScript = `
                                            const localStorageData = JSON.stringify(localStorage)
                                            const sessionStorageData = JSON.stringify(sessionStorage)
                                            return {'next':false,'init':false,data:{'localData':localStorageData,'sessionData':sessionStorageData}}`;
                                var crawlerWebContents = mainWindow.webContents;
                                jsExpress.executeUIEventScript(processTask, uiEventScript, crawlerWebContents, {
                                    'init': true,
                                    data: {}
                                }, async function (processTask, result) {

                                }, async function (processTask, result) {  ///最终执行
                                    console.log('executeUIEventScript final execute start write store to file.');

                                    let resData = result.data.data;

                                    let appData = {
                                        localData: resData.localData,
                                        sessionData: resData.sessionData,
                                        cookieData: ''
                                    };

                                    // let dataToWrite = result.data.data.localData;
                                    // let filePath = config.local_store_file_path;
                                    // // 获取文件的父级文件夹路径
                                    // let folderPath = path.dirname(filePath);
                                    // // 使用 fs.mkdirSync 创建父级文件夹（如果不存在）
                                    // // fs.mkdirSync(folderPath, { recursive: true });
                                    // fs.writeFile(filePath, dataToWrite, (err) => {
                                    //     if (err) {
                                    //         console.error('Error writing file:', err);
                                    //     } else {
                                    //         console.log('local storage File has been written successfully.');
                                    //     }
                                    // });
                                    // dataToWrite = result.data.data.sessionData;
                                    // filePath = config.session_store_file_path;
                                    // // 获取文件的父级文件夹路径
                                    // folderPath = path.dirname(filePath);
                                    // // 使用 fs.mkdirSync 创建父级文件夹（如果不存在）
                                    // // fs.mkdirSync(folderPath, { recursive: true });
                                    // fs.writeFile(filePath, dataToWrite, (err) => {
                                    //     if (err) {
                                    //         console.error('Error writing file:', err);
                                    //     } else {
                                    //         console.log('session storage File has been written successfully.');
                                    //     }
                                    // });

                                    const ses = crawlerWebContents.session;
                                    let url = 'https://www.moyin.com/'
                                    // 获取当前窗口的 cookies

                                    const cookies = await ses.cookies.get({url: url});
                                    var ckarray = cookies.map(ck => {
                                        return ck.name + "=" + ck.value
                                    })
                                    let cookieStr = ckarray.join(";");
                                    appData.cookieData = cookieStr;

                                    let reqUrl = config.remote_http_url + 'system/setApplicationData'
                                    netRequest.request({
                                        url: reqUrl,
                                        method: 'post',
                                        data: appData
                                    }).then(async function (result) {
                                        if (result.status == 200) {
                                            let resData = result.data;
                                            console.log('setApplicationData result=', resData)
                                            if (resData.success) {
                                                logger.info('setApplicationData success');
                                            }
                                        }
                                    }).catch(function (error) {
                                        console.error('setApplicationData error=', error);
                                    })

                                });
                            }

                            //排行榜
                        } else if (requestMap[params.requestId].startsWith('https://apphyamvcvu5184.h5.xiaoeknow.com/xe.data.learn_center.get_user_rank_list/1.0.0')) {  //登录结果
                            var data = response.base64Encoded ? Buffer.from(response.body, 'base64').toString() : response.body;
                            //不管成功或失败，将获得的 cookie 发送到 web服务中心 todo
                            console.log(data)


                            const ses = session.defaultSession;

                            // // 指定要获取Cookie的域名
                            // const domain = 'bilibili.com';
                            // // 使用cookies属性获取域名对应的Cookie
                            // ses.cookies.get({ domain }).then((cookies) => {
                            //     console.log('Cookies for domain', domain, ':', cookies);
                            //     var ckarray = cookies.map(ck => {
                            //         return ck.name + "=" + ck.value
                            //     })
                            //     let cookie_str = ckarray.join(";");
                            //     console.log('Cookies after:', cookie_str);
                            //     //
                            //     let request_set_token_url = api_base_path+'system/setToken';
                            //     let winObject = getWinObjectByWebContentsId(webview.webContents.id);
                            //     let user = winObject.data.user;
                            //     netRequest.request({url:request_set_token_url,method:'post',data:{"user":user,"cookie":cookie_str}}).then(async function (result) {
                            //         winObject.status == 0;  //状态为空闲,后续优化成异步监控处理 todo
                            //         winObject.instance.close();
                            //
                            //     }).catch(function(error){
                            //         console.error('submitImgData error=',error);
                            //     })
                            // }).catch((error) => {
                            //     console.error('Error getting cookies:', error);
                            // });


                        }
                        // console.log(response);
                        // console.log(1111);
                        // console.log(requestMap[params.requestId])
                        // var data = JSON.parse(response.base64Encoded ? Buffer.from(response.body, 'base64').toString():response.body)

                        // const responseBody = data.toString('utf-8');

                        // var processTask = getProcessTaskByWebContentsId(webContentsId);
                        // var webContents = processTask.winObject.webview.webContents;
                        // jsExpress.executeExtractRequestJs(processTask, data, webContents, function (resultData) {
                        //     // resultData.url = task.extractDataUrl;
                        //     // resultData.id = task.id;,
                        //     resultData.id = processTask.currentTask.id;
                        //     submitTaskResult(resultData, webContentsId, 1);  //暂时每次一次
                        // });
                    });

                } catch (e) {
                    console.error(e);
                }
            }
        }
    });


}

function loadCacheToApplication() {

    let processTask = {status: 1, analyseExpressType: 0,};
    //请求http接口 getApplicationData
    let reqUrl = config.remote_http_url + 'system/getApplicationData'
    netRequest.request({url: reqUrl, method: 'get'}).then(async function (result) {
        if (result.status == 200) {
            let resData = result.data;
            if (resData.success) {
                console.log('getApplicationData result=', resData.data)
                // {localData:localData,sessionData:sessionData,cookieData:cookieData}


                let cookieString = resData.data.cookieData;
                if (cookieString) {
                    // 解析 Cookie 字符串并设置到 session
                    // console.log('cookiestr before=' + cookieString)
                    const ses = mainWindow.webContents.session;
                    let url = 'https://www.moyin.com/'
                    cookieString.split(';').map(cookie => {
                        const [name, value] = cookie.trim().split('=');
                        var ck = {url: url, name, value};
                        ses.cookies.set(ck, (error) => {
                            if (error) {
                                console.error('Failed to set cookies:', error);
                            } else {
                                console.log('Cookies set successfully!');
                            }
                        });
                    });
                }
                let local_store_content = resData.data.localData;
                let session_store_content = resData.data.sessionData;
                let uiEventScript = `
                    const localStorageData = JSON.parse(Base64.decode('` + Base64.encode(local_store_content) + `'));
                    
                    for (const key in localStorageData) {
                        let val = localStorageData[key];
                        // if(val.indexOf('{') >=0){
                        //    val = JSON.parse(val);
                        // }
                        localStorage.setItem(key,val);
                    }
                    const sessionStorageData = JSON.parse(Base64.decode('` + Base64.encode(session_store_content) + `'));
                    for (const key in sessionStorageData) {
                        let val = sessionStorageData[key];
                        // if(val.indexOf('{') >=0){
                        //    val = JSON.parse(val);
                        // }
                        sessionStorage.setItem(key,val);
                    }
                    return {'next':false,'init':false,data:{}}`;

                var crawlerWebContents = mainWindow.webContents;
                // debugger
                // let sleep_seconds = system.getRandomInt(3, 5) * 1000;
                // await system.sleep(sleep_seconds);
                jsExpress.executeUIEventScript(processTask, uiEventScript, crawlerWebContents, {
                    'init': true,
                    data: {}
                }, async function (processTask, result) {
                    if (result.data.data.execTimes == 1) {
                    }

                }, function (processTask, result) {  ///最终执行
                    console.log('executeUIEventScript final execute start. read store from file done');

                    logger.info(`read store from file result=${JSON.stringify(result)}`)

                    mainWindow.reload();

                });

            }
        }
    }).catch(function (error) {
        console.error('getApplicationData error=', error);
    })
}

//
function indexPageLoaded() {
    logger.info(`indexPageLoaded start`);
    // initMainWindow();

    // loadCacheToApplication();

}

function mainForeRefresh() {
    logger.info(`mainForeRefresh start`);
    // loadCacheToApplication();

}


function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


// function requestByHttp(webContentsId,url,method,data,headers,pageNo){
//     // let headers = JSON.parse(processTask.requestHeaders);
//     // let postData = processTask.requestBody;
//     let reqUrl = url.replace('${page}',pageNo);
//     let postData = data;
//     if(postData){
//         postData = postData.replace('${page}',pageNo);
//     }
//     netRequest.request({url:reqUrl,method:method,data:postData,headers:headers}).then(function(result){
//         //如果要解析数据
//         //debugger;
//         let executeParamData = result.data;
//         if(typeof(executeParamData) == 'object'){
//             executeParamData = JSON.stringify(executeParamData);
//         }
//         var processTask = getProcessTaskByWebContentsId(webContentsId);
//         var webContents = processTask.winObject.instance.webContents;
//         jsExpress.executeExtractRequestJs(processTask, executeParamData, webContents, function (resultData) {
//             // resultData.url = task.extractDataUrl;
//             // resultData.id = task.id;,
//             if(!resultData.success && resultData.errorStatus == 0){ //ban todo
//
//             }
//             // let responseAnalyseBody = resultData.responseAnalyseBody;
//             let responseEmpty = resultData.responseEmpty;
//             let maxPage = processTask.variableMap['maxPage'] || 1000000;
//             maxPage = parseInt(maxPage);
//             resultData.id = processTask.currentTask.id;
//             if(processTask.sliceActive == 1){ //分页处理或切片处理
//                 if(!responseEmpty && pageNo < maxPage){//下一页
//                     submitTaskResult(resultData, webContentsId, 1);  //切片提交
//                     pageNo++;
//                     // let reqUrl = url.replace('${page}',pageNo);
//                     // let postData = data.replace('${page}',pageNo);
//                     const delayInSeconds = getRandomInt(2, 5);
//                     setTimeout(() => {
//                         requestByHttp(webContentsId,url,method,data,headers,pageNo);
//                     }, delayInSeconds * 1000);
//                 }else{
//                     submitTaskResult(resultData, webContentsId, 2);  //最终提交
//                 }
//             }else{
//                 submitTaskResult(resultData, webContentsId, 0);  //普通提交
//             }
//         });
//     }).catch(function(error){
//         console.error(error);
//     });
// }

function updateProcessTaskStatus(activeProcessId, status) {
    let processTask = getProcessTaskByWebContentsId(activeProcessId);
    if (processTask != null) {
        processTask.status = status;

        let collectStatus = status == -1 ? false : true;
        webSocketServer.sendMessage('collectStatus', collectStatus, processTask.webContentsId)
    }
}

function exportToExcel(parentWindow,dataToExport,fileName,successCallback) {

    // Example data to export to Excel
    // const dataToExport = [
    //     ['Name', 'Age', 'Country'],
    //     ['John', 25, 'USA'],
    //     ['Alice', 30, 'Canada'],
    //     ['Bob', 22, 'UK']
    // ];


    const wb = xlsx.utils.book_new();
    const ws = xlsx.utils.aoa_to_sheet(dataToExport);
    xlsx.utils.book_append_sheet(wb, ws, 'Sheet 1');

    const excelBuffer = xlsx.write(wb, { bookType: 'xlsx', type: 'buffer' });

    dialog.showSaveDialog(parentWindow, {
        title: '保存文件',
        defaultPath: path.join(app.getPath('downloads'), fileName),
        filters: [
            { name: 'Excel Files', extensions: ['xlsx'] }
        ]
    }).then(result => {
        if (!result.canceled) {
            const filePath = result.filePath;
            fs.writeFileSync(filePath, excelBuffer);
            successCallback();
        }
    }).catch(err => {
        console.error(err);
    });
}


function closeMainWindow(yesCallback, noCallback) {
    // 显示确认对话框
    const choice = dialog.showMessageBoxSync(mainWindow, {
        type: 'question',
        buttons: ['是', '否'],
        title: '确认关闭',
        message: '您确认需要关闭吗？',
    });

    if (choice === 0) {
        // 用户点击 "是"，可以关闭窗口
        mainWindow = null;
        //向服务端提交close任务【并且是同步进行？】todo
        if (yesCallback) {
            beforeCloseMainWindow(yesCallback);
        }

    } else {
        if (noCallback) {
            noCallback();
        }
    }
}


function beforeCloseMainWindow(callback) {
    if (checkTaskJob) {
        checkTaskJob.cancel();
    }
    if (monitorWinJob) {
        monitorWinJob.cancel();
    }
    webSocketServer.closeAll();
    callback();
}

function closeCollectWindow(winObject, event) {
    //如果有正在 进行的任务，则提示
    let needTip = winObject.processTask && winObject.processTask.status == 1;
    if (needTip) {
        // 显示确认对话框
        const choice = dialog.showMessageBoxSync(winObject.instance, {
            type: 'question',
            buttons: ['是', '否'],
            title: '确认关闭',
            message: '该有正在进行的任务，关闭窗口将终止任务，您确认需要关闭吗？',
        });

        if (choice === 0) {
            // 用户点击 "是"，可以关闭窗口
            beforeCloseCollectWindow(winObject);

        } else {
            // 阻止窗口关闭
            event.preventDefault();
        }
    } else {
        beforeCloseCollectWindow(winObject);
    }
}

function beforeCloseCollectWindow(winObject) {
    webSocketServer.close(winObject.webContentsId);
    winPoolArray.removeFirst('id', winObject.id);

    //向服务端提交close任务【并且是同步进行？】todo
}


// 导出模块函数
module.exports = {
    start,
    closeMainWindow,
    indexPageLoaded,
    mainForeRefresh,

};

